package view.Windows;

import java.io.ObjectInputStream.GetField;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Monitor;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import view.General.LegenDateGUI;

import commons.Constants;

import controller.LegenDateManager;


public class InsertDBInfoGUI {

	private static final Log logger = LogFactory.getLog(LoginGUI.class);

	final protected Shell shlMain;
	protected Color cBackgroundColor;
	protected Font fHeadFont;
	protected Font fLabelFont;
	protected Font fButtonFont;
	protected ProgressBar pbDBUpdate;

	public InsertDBInfoGUI(final Display mainDisp) {

		initFonts(mainDisp);

		shlMain = new Shell(mainDisp, SWT.SHELL_TRIM & ~SWT.RESIZE & SWT.CLOSE & ~SWT.MAX);
		shlMain.setBounds(10, 10, 450, 300);

		// locate the shell in the center of the display
		Monitor primary = mainDisp.getPrimaryMonitor();
		Rectangle bounds = primary.getBounds();
		Rectangle rect = shlMain.getBounds();
		int x = bounds.x + (bounds.width - rect.width) / 2;
		int y = bounds.y + (bounds.height - rect.height) / 2;
		shlMain.setLocation(x, y);

		// main definitions
		shlMain.setText("LegenDate - Insert DB Definitions!");
		ImageData id = new ImageData(getClass().getClassLoader().getResourceAsStream("view/Assets/LegenDate.png"));
		Image icon = new Image(mainDisp, id);
		shlMain.setImage(icon);
		Color clrWhite = new Color(mainDisp, 255, 255, 255);
		Image imgBackground = new Image(mainDisp, getClass().getClassLoader().getResourceAsStream("view/Assets/LegenDateBG2.jpg"));
		shlMain.setBackgroundImage(imgBackground);
		shlMain.setBackgroundMode(SWT.INHERIT_DEFAULT);

		Label lblName = new Label(shlMain, SWT.LEFT);
		lblName.setText("Enter DB definitions:");
		lblName.setBounds(25, 30, 210, 20);
		lblName.setForeground(clrWhite);

		// connection URL
		Label lblConnectionURL = new Label(shlMain, 0);
		lblConnectionURL.setText("Connection URL: ");
		lblConnectionURL.setBounds(25, 65, 135, 20);
		lblConnectionURL.setForeground(clrWhite);
		lblConnectionURL.setFont(getLabelFont());

		final Text txConnectionURL = new Text(shlMain, SWT.BORDER);
		txConnectionURL.setBounds(165, 65, 240, 20);
		txConnectionURL.setText(Constants.DB_URL);

		// user name
		Label lblUser = new Label(shlMain, 0);
		lblUser.setText("DB UserName: ");
		lblUser.setBounds(25, 105, 135, 20);
		lblUser.setForeground(clrWhite);
		lblUser.setFont(getLabelFont());

		final Text txUser = new Text(shlMain, SWT.BORDER);
		txUser.setBounds(165, 105, 240, 20);
		txUser.setText(Constants.DB_USERNAME);
		
		// password
		Label lblPassword = new Label(shlMain, 0);
		lblPassword.setText("DB Password: ");
		lblPassword.setBounds(25, 145, 135, 20);
		lblPassword.setForeground(clrWhite);
		lblPassword.setFont(getLabelFont());

		final Text txPassword = new Text(shlMain, SWT.BORDER | SWT.PASSWORD);
		txPassword.setBounds(165, 145, 240, 20);
		txPassword.setText(Constants.DB_PASSWORD);

		// open LegenDate button
		Button btnLogin = new Button(shlMain, SWT.NONE);
		btnLogin.setFont(getButtonFont());
		btnLogin.setText("Open LegenDate!");
		btnLogin.setBounds(150, 220, 150, 30);
		btnLogin.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				LegenDateGUI.get().setIsLogin(true);
				LegenDateManager.updateDB(txConnectionURL.getText(), txUser.getText(), txPassword.getText());

				shlMain.close();
				shlMain.dispose();
			}
		});
		
		// open LegenDate when pressing 'enter'
		shlMain.addListener(SWT.Traverse, new Listener() {

			public void handleEvent(Event event) {
				switch (event.detail) {
					case SWT.TRAVERSE_RETURN:
						LegenDateGUI.get().setIsLogin(true);
						LegenDateManager.updateDB(txConnectionURL.getText(), txUser.getText(), txPassword.getText());
						
						shlMain.close();
						shlMain.dispose();
				}
			}
		});
		
	}

	public Color getBackgroundColor() {
		return cBackgroundColor;
	}

	public void setBackgroundColor(Color cBackgroundColor) {
		this.cBackgroundColor = cBackgroundColor;
	}

	public Font getHeadFont() {
		return fHeadFont;
	}

	public void setHeadFont(Font fHeadFont) {
		this.fHeadFont = fHeadFont;
	}

	public Font getLabelFont() {
		return fLabelFont;
	}

	public void setLabelFont(Font fLabelFont) {
		this.fLabelFont = fLabelFont;
	}

	public Font getButtonFont() {
		return fButtonFont;
	}

	public void setButtonFont(Font fButtonFont) {
		this.fButtonFont = fButtonFont;
	}

	/**
	 * initialize fonts
	 * 
	 * @param display
	 */
	protected void initFonts(Display display) {
		setHeadFont(new Font(display, new FontData("Fiolex Girls", 20, SWT.BOLD)));
		setLabelFont(new Font(display, new FontData("Ariel", 10, SWT.BOLD)));
		setButtonFont(new Font(display, new FontData("Ariel", 10, SWT.BOLD)));

	}

	/**
	 * show the shell
	 * 
	 * @param display
	 */
	public void showGUI(Display display) {
		shlMain.open();
		// this is what's holding the shell open!!!
		while (!shlMain.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
	}

}
